package com.fjrcloud.service;

import com.fjrcloud.dao.AppendagesDao;
import com.fjrcloud.dao.SubsidiesOldManDao;
import com.fjrcloud.entity.Appendages;
import com.fjrcloud.entity.HouseholdMaster;
import com.fjrcloud.entity.SubsidiesOldMan;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 17-2-20.
 */
@Service
public class OtherAppendagesService {

    @Autowired
    private AppendagesDao appendagesDao;
    @Autowired
    private SubsidiesOldManDao subsidiesOldManDao;
    @Autowired
    private HouseholdService service;

    public Appendages getAppendages(Integer id){
        return appendagesDao.findOne(id);
    }

    public void delAppendages(Integer id){
        appendagesDao.delete(id);
    }

    public Appendages saveAppendages(Appendages entity,Integer masterId) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("无认定主体");
        HouseholdMaster master = service.getMaster(masterId);
        if (master == null)
            throw new BusinessException("认定主体无效");

        Appendages old = null;
        if (entity.getId() != null)
            old = appendagesDao.findOne(entity.getId());

        if (old != null){
            entity.setId(old.getId());
        }
        entity.setMaster(master);

        return appendagesDao.save(entity);
    }

    private PageRequest findPage(Integer pageNum, Integer pageSize) {
        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return pageRequest;
    }
    public Page<Appendages> findAppendages(Integer masterId,Integer pageNum, Integer pageSize) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("无认定主体");
        HouseholdMaster master = service.getMaster(masterId);
        if (master == null)
            throw new BusinessException("认定主体无效");

        return appendagesDao.findByMaster(master,findPage(pageNum,pageSize));
    }

    public SubsidiesOldMan getSubsidiesOldMan(Integer id){
        return subsidiesOldManDao.findOne(id);
    }

    public void delSubsidiesOldMan(Integer id){
        subsidiesOldManDao.delete(id);
    }

    public SubsidiesOldMan saveSubsidiesOldMan(SubsidiesOldMan entity,Integer masterId) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("无认定主体");
        HouseholdMaster master = service.getMaster(masterId);
        if (master == null)
            throw new BusinessException("认定主体无效");

        SubsidiesOldMan old = null;
        if (entity.getId() != null)
            old = subsidiesOldManDao.findOne(entity.getId());

        if (old != null){
            entity.setId(old.getId());
        }
        entity.setMaster(master);

        return subsidiesOldManDao.save(entity);
    }

    public Page<SubsidiesOldMan> findSubsidiesOldMan(Integer masterId, Integer pageNum, Integer pageSize) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("无认定主体");
        HouseholdMaster master = service.getMaster(masterId);
        if (master == null)
            throw new BusinessException("认定主体无效");

        return subsidiesOldManDao.findByMaster(master,findPage(pageNum,pageSize));
    }
}
